package com.power.structure.array;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/11 11:23 上午
 */

import java.util.Arrays;

/**
 * 数组的定义
 * 容量的定义
 * 扩展因子的定义
 * 判断空条件
 * 判断满条件
 */
public class PowerArray {
    private int CAPACITY = 10;
    private float FACCTORY = 0.75f;
    int data[];
    int size = 0;

    public PowerArray() {
        data = new int[CAPACITY];
    }

    public PowerArray(int capacity) {
        this();
        CAPACITY = capacity;
    }

    public void add(int element) {
        if (isFull()) {
            resize();
        }
        data[size++] = element;
    }

    public void resize() {
        int newCapcity = CAPACITY + (int) (CAPACITY * FACCTORY);
        data = Arrays.copyOf(data, newCapcity);
        CAPACITY = newCapcity;
    }

    public boolean rangeCheck(int index) {
        //如果索引大于容量返回false
        if (index > size) {
            throw new IndexOutOfBoundsException("数组越界了");
        }
        return true;
    }

    public void replace(int index, int elemenet) {
        //Todo
        if (rangeCheck(index)) {
            data[index] = elemenet;
            return;
        }
        throw new IndexOutOfBoundsException("索引越界了");

    }


    public void remove(int element) {
        //Todo
        if (isEmpty()) {
            throw new IndexOutOfBoundsException("没有数据可以移除了");
        }
        for (int i = 0; i < size; i++) {
            if (data[i] == element) {
                System.arraycopy(data, i + 1, data, i, size - 1 - i);
                size--;
            }
        }
    }

    public boolean isEmpty() {
        if (size == 0)
            return true;
        return false;
    }

    public boolean isFull() {
        if (size == CAPACITY)
            return true;
        return false;

    }

    public void display(int[] a) {
        for (int i = 0; i < size; i++) {
            System.out.print(" " + a[i]);
        }
        System.out.println("\n");
    }

    public static void main(String[] args) {
        PowerArray powerArray = new PowerArray();
        for (int i = 0; i < 30; i++) {
            powerArray.add(i);
        }
        powerArray.display(powerArray.data);


        for (int i = 0; i < powerArray.size; i++) {
            if (i % 3 == 0) {
                powerArray.remove(i);
            }
            powerArray.display(powerArray.data);
        }
        System.out.println("\n" + powerArray.size);

        for (int i = 0; i < powerArray.size; i++) {
            powerArray.remove(i);
        }
        powerArray.replace(3, 100);
        powerArray.replace(23, 100);
        powerArray.display(powerArray.data);
    }

}
